package com.hanson.demo.queue.memory;

import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.hanson.demo.actors.Message;
/**
 * 模拟一个kafka 存储器,存储消息
 * @author hanson
 *
 */
public class InMemoryStorage {
	private static InMemoryStorage instance;
	private final ConcurrentLinkedQueue<Map<String, Message>> storage;

	private InMemoryStorage() {
		storage = new ConcurrentLinkedQueue<>();
	}

	public static InMemoryStorage getInstance() {
		if (instance == null) {
			synchronized (InMemoryStorage.class) {
				if (instance == null) {
					instance = new InMemoryStorage();
				}
			}
		}
		return instance;
	}
	public boolean put(String topic, Message msg) {
        return storage.add(Map.of(topic,msg));
    }
	public Map<String,Message> poll() {
        return storage.poll();
    }
}
